<?php

/**
 * File Name：PublicController.php
 * File Encoding：UTF-8
 * File New Time：2014-8-5 11:22:39
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class PublicController extends YzwController {

    //发送短信验证码
    public function actionSnscode() {
        $moblie = Yii::app()->request->getParam('moblie');
        if (empty($moblie)) {
            $this->error('手机号不能为空！');
        }
        $key = md5(GuestDataModel::model()->getForwardedForIp());
        if (Yii::app()->cache->get($key)) {
            $this->error('请60秒后再次发送！');
        }
        if (defined('YII_DEBUG') && YII_DEBUG == false) {
            $rKey = md5($moblie);
            $redisKey = (int) RedisCluster::getInstance()->get($rKey);
            if ($redisKey >= 5) {
                $this->error('该手机号今日已达最大发送数！');
            }
        }
        if (SnsVerification::getInstance()->send($moblie, 'cellphone')) {
            if (defined('YII_DEBUG') && YII_DEBUG == false) {
                $redisKey = (int) $redisKey + 1;
                $time = strtotime(date('Y-m-d 23:59:59')) - time();
                RedisCluster::getInstance()->set($rKey, $redisKey, $time);
            }
            Yii::app()->cache->set($key, 1, 60);
            $this->success('短信验证码已经发送！');
        } else {
            $this->error('验证码发送失败！');
        }
    }

    //分享商品增加积分
    public function actionShare() {
        $goods_id = Yii::app()->request->getParam('goods_id');
        if (!Yii::app()->user->isLogged()) {
            $this->error('用户未登录');
        }
        $uid = Yii::app()->user->id;
        $objGoodsModel = GoodsModel::model()->findByPk($goods_id);
        if (!$objGoodsModel) {
            $this->error('商品信息错误');
        }
        //当前商品分享标识
        $key = "RedeemgoodController_Share_{$goods_id}_{$uid}";
        //记录此用户今天分享商品的次数，超过30次则不再送积分
        $key_count = "RedeemgoodController_Share_count_{$uid}";
        $cache = RedisCluster::getInstance()->get($key);
        if (!empty($cache)) { //今天分享过
            $this->error('今天已经分享过此商品');
        }
        $cache_count = RedisCluster::getInstance()->get($key_count);
        if (!empty($cache_count) && $cache_count >= 30) {
            $this->error('今天分享的商品已经达到30个');
        }
        $time = time();
        $endTime = strtotime(date('Y-m-d 23:59:59')); //今天结束时间
        //增加积分
        if (MembersModel::model()->plusScore($uid, 5) == true) {
            MembersActionModel::model()->addShareAction($uid, '分享商品送积分活动，赠送积分 5 。');
            //经验值
            MembersModel::model()->plusExp($uid, 1, '分享商品，经验值加1。');
            //表示已经分享过
            RedisCluster::getInstance()->set($key, 1, $endTime - $time);
            RedisCluster::getInstance()->set($key_count, $cache_count + 1, $endTime - $time);
            $this->success('增加分享积分成功！');
        } else {
            $this->error('积分增加失败！');
        }
    }

    //分享页面增加积分
    public function actionPageShare() {
        if (!Yii::app()->user->isLogged()) {
            $this->error('用户未登录');
        }
        $uid = Yii::app()->user->id;
        $key = "RedeemgoodController_Share_page_{$uid}";
        $cache = RedisCluster::getInstance()->get($key);
        if (!empty($cache)) {
            $this->error('今天已经分享过此页面');
        }
        $time = time();
        $endTime = strtotime(date('Y-m-d 23:59:59')); //今天结束时间
        //增加积分
        if (MembersModel::model()->plusScore($uid, 10) == true) {
            MembersActionModel::model()->addShareAction($uid, '分享页面送积分活动，赠送积分 10 。');
            //经验值
            //MembersModel::model()->plusExp($uid, 1, '分享页面，经验值加1。');
            RedisCluster::getInstance()->set($key, 1, $endTime - $time);
            $this->success('增加分享积分成功！');
        } else {
            $this->error('积分增加失败！');
        }
    }

    //分享积分记录商品
    public function actionRecordgoodShare() {
        if (!Yii::app()->user->isLogged()) {
            $this->error('用户未登录');
        }
        $id = Yii::app()->request->getParam('id');
        $uid = Yii::app()->user->id;
        $objRedeemRecordsModel = RedeemRecordsModel::model()->findByPk($id);
        if (!$objRedeemRecordsModel) {
            $this->error('找不到记录');
        }
        if ($objRedeemRecordsModel->is_share) {
            $this->error('已经分享过');
        }
        //增加积分
        if (MembersModel::model()->plusScore($uid, 10) == true) {
            MembersActionModel::model()->addShareAction($uid, '分享积分记录商品送积分活动，赠送积分 10 。');
            $objRedeemRecordsModel->is_share = 1;
            $objRedeemRecordsModel->save(false, array('is_share'));
            //经验值
            //MembersModel::model()->plusExp($uid, 1, '分享商品，经验值加1。');
            $this->success('增加分享积分成功！');
        } else {
            $this->error('积分增加失败！');
        }
    }

}
